$(document).ready(function () {

    $.ajax({
        url: "http://"+urla+"/LoginStatusServlet",
        //url: "./json/categories/parent/0/list.json",
        type: "GET",
        dataType: 'JSON',
        success: function(result){
            console.log(result);
            if (result.status!=1){
                window.location.replace("main.html");
            }
        }
    });

    var successModal=$('#successModal');
    var unknownModal=$('#unknownModal');
    var btn_login=$("#registerSub");

    btn_login.on("click",function (event) {
        event.preventDefault();//使a自带的方法失效
        $.ajax({
            type: "POST",
            cache: true,
            url:"http://"+urla+"/RegisterServlet",
            dataType: "json",
            data:{
                "nickname":$("#username").val(),
                "password":$("#password").val(),
                "email":$("#email").val(),
                "phone":$("#phone").val()
            },
            error: function(request) {
                alert("Connection error");
            },
            success: function(data) {
                console.log(data)
                if (data.status===0){
                    successModal.modal('show');
                    successModal.css("padding-right","0");
                    $("#successModal .alert").text(data.msg+"立即登录");
                    successModal.on('hide.bs.modal', function () {
                        window.location.replace("login.html?userId="+data.user_id);
                    })
                }else {
                    unknownModal.modal('show');
                    unknownModal.css("padding-right","0");
                    $("#unknownModal .alert").text(data.msg+"请重试");
                    unknownModal.on('hide.bs.modal', function () {
                        location.reload();
                    })
                }

            }
        });
    });





    var InputUsername=$("#username");
    InputUsername.val("");
    var InputUserPsd=$("#password");
    InputUserPsd.val("");
    var InputUserPsd2=$("#password2");
    InputUserPsd2.val("");
    var email=$("#email");
    email.val("");
    var phone=$("#phone");
    phone.val("");
    btn_login.attr("disabled", "disabled");
    btn_login.attr("class", "btn btn-block btn-lg ");

    $(document).bind('input propertychange', function(){
        var user=InputUsername.val();
        var psd=InputUserPsd.val();
        var psd2=InputUserPsd2.val();
        var e=email.val();
        var p=phone.val();

        //判断输入为空禁用按钮
        if (user===""||psd===""||psd2===""||psd!==psd2) {
            btn_login.attr("class", "btn btn-block btn-lg");
            btn_login.attr("disabled","disabled");
        } else {
            //判断号码输入格式，不满足则提示
            if(!((/^1[3|4|5|7|8]\d{9}$/.test(p))&&e.match(/^\w+@\w+\.\w+$/i))){
                console.log("phone false")
                btn_login.attr("class", "btn btn-block btn-lg");
                btn_login.attr("disabled","disabled");
                $("#registerSub").html("请检查输入的邮箱与号码格式")
            }else {
            $.ajax({
                url: "http://"+urla+"/PhoneAndEmailCheckServlet",
                type:"POST",
                dataType:"json",
                data: {
                    "phone":p,
                    "email":e,
                },
                success:function (data){
                    $("#phoneTip").hide();
                    $("#emailTip").hide();
                    if (data.status==1){
                        btn_login.attr("class", "btn btn-block btn-lg");
                        btn_login.attr("disabled","disabled");
                        if (data.email==data.phone){
                            $("#emailTip").show();
                            $("#phoneTip").show();
                        }else {

                            if (data.phone ==1){
                                $("#phoneTip").show();
                                $("#emailTip").hide();
                            }else {
                                $("#emailTip").show();
                                $("#phoneTip").hide();
                            }
                        }
                    }
                }
            })
                console.log("phone ture")
                btn_login.removeAttr("disabled");
                btn_login.attr("class", "btn  btn-success btn-block btn-lg");
                $("#registerSub").html("注   册");
                // $("#phoneTip").hide();
            }
        }

        // emailListen();

        if (psd!==psd2){
            $("#psdTip").show();
        }else {
            $("#psdTip").hide();
        }



    });


    //判断号码是否正确

    //判断邮箱是否正确
    function emailListen() {
        $("#email").keyup(function () {
            var e=email.val();
            if(e.match(/^\w+@\w+\.\w+$/i)){
                console.log("email true")
                btn_login.removeAttr("disabled");
                btn_login.attr("class", "btn  btn-success btn-block btn-lg");
                $("#emailTip").hide();
                // alert('邮箱格式正确！')
            }else{
                console.log("email false");
                btn_login.attr("class", "btn btn-block btn-lg");
                btn_login.attr("disabled","disabled");
                $("#emailTip").show();
                // alert('邮箱格式错误！')
            }
        })
    }

});